<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 闪现付3
 */
class Shanxianfu3Controller extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));

        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();

        $return = $this->orderadd( $parameter );

        $data = [];
        $data['GoodsName'] = 'shx';
        $data['MerchantCode'] = $this->config['mch_id'];
        $data['Money'] = intval($return['amount']*100);
        switch($this->config['paytype']) {
            //微信
            case 1:
                $data['PayType'] = 'WxPay';
                break;
            case 3:
            case 4:
                $data['PayType'] = 'AlPay';
                break;
            case 17:
            case 18:
                $data['PayType'] = 'AlWap';
                break;
            case 13:
                $data['PayType'] = 'AlPay';
                break;
            default:
                $this->showmessage('暂不支持此支付方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['Order'] = $return['orderid'];
        $data['ClientIp'] = getIP();
        $data['SynUrl'] = $return['callbackurl'];
        $data['Sign'] = $this->getSign($data);

        $res = $origin = curl($this->config['gateway'], httpstr($data));

        $res = json_decode($res, true);

        //echo '<pre/>'; print_r($data); print_r($res); exit;

        $code = $res['PayUrl'];
        if(!$code){
            if($res['ErorMsg']){
                $this->showmessage($res['ErorMsg'],['code'=>C("THIRDCODES.".$this->class)]);
            }

            if($origin){
                $this->showmessage($origin,['code'=>C("THIRDCODES.".$this->class)]);
            }

            $this->showmessage('获取支付链接失败!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        //echo "<pre/>"; print_r($res); exit;


        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=".base64_encode($code));

        switch($this->config['paytype']){
            //微信
            case 1:
                header("location: {$code}"); exit;
                if($isMobile){
                    $this->display("WeiXin/weixinwap");
                }else{
                    $this->display("WeiXin/weixin");
                }
                break;
            //支付宝扫码
            case 3:
                //支付宝H5
            case 4:
            case 17:
            case 18:
                header("location: {$code}"); exit;
                if(isMobile()){
                    $this->display("WeiXin/alipaywap");
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    private function getSign($data){
        ksort($data);

        $str = '';
        foreach($data as $k=>$v){
            if($v === '' || $k==='Sign'){
                continue;
            }

            $str .= "{$k}={$v}&";
        }

        $str .= "Key=" . $this->config['signkey'];

        return md5($str);
    }

    //通知
    public function notifyurl(){
		//$this->debug_callback(); exit("ok");
        if(!$this->checkip(getIP(),['43.243.93.89'])){
            exit("wrong");
        }

        $p = I("post.");

        /*$p = array (
            'Order' => '2019040321194686524',
            'Money' => '1000',
            'ThirdPartyOrder' => '',
            'Sign' => '107fc26986d353cdb01313f23c814d61',
        );*/

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['Order'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
      /*  M("Order")->where(array("pay_orderid"=>$orderid))->save(array(


            //"pay_orderid_rd"=>$p['prdOrdNo']
        ));*/

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['Sign'];
        $sign2 = $this->getSign($p);

        //echo "sign:".$sign."<br/>";
        //echo "sign2:".$sign2;exit;

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        $this->async($find['pay_orderid'],"SUCCESS");
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}